package org.vectrics.mail.struts;

import javax.servlet.jsp.JspException;

import org.vectrics.mail.FileEmailTemplateCategory;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.VectricsActionData;
import org.vectrics.struts.VectricsStrutsAction;

public class TemplateCategoryEditAction extends VectricsStrutsAction {

	@Override
	protected String doExecute(VectricsActionData actionData)
			throws JspException {
		String catIdParam = actionData.getRequest().getParameter("categoryId");
		if (catIdParam == null) {
			log.debug("New category");
			EmailTemplateCategoryForm form = new EmailTemplateCategoryForm();
			actionData.setRequestForm(form);
			
		} else {
			Long categoryId = new Long(catIdParam);
			FileEmailTemplateCategory category = CoreServiceLocator.getSystemService().findFileEmailTemplateCategory(categoryId);
			log.info("Category = " + category);
			EmailTemplateCategoryForm form = new EmailTemplateCategoryForm();
			form.setId(category.getId().toString());
			form.setCode(category.getCode());
			form.setDescription(category.getDescription());
			actionData.setRequestForm(form);
		}
		
		return "success";
	}

}
